<?php
namespace app\api\model;

use app\BaseModel;
use think\Db;

class BbsModel extends BaseModel {

	/**
	 * 得到BBs所有的category
	 */
	function findAllCategory(){
		//得到列表
		$sliderList = Db::name('t_category')->where("pid","1")->order('orderid', 'desc')->select();
		return $sliderList;
	}
	  
	 
	 /**
	 * 得到BBs板块
	 */
	function findBBsentityListByCategoryId($categoryId){
		//得到列表
		$sliderList = Db::name('t_bbs_entity')->where("categoryId",$categoryId)->order('orderid', 'desc')->select();
		return $sliderList;
	}
	
	 /**
	 * 得到后台设置推荐的论坛
	 */
	function findBBsentityImportantList(){
		//得到列表
		$bbsList = Db::name('t_bbs_entity')->where("isImportant",TRUE)->order('orderid', 'desc')->select();
		return $bbsList;
	}
	
	 /**
	 * 搜索论坛
	 */
	function bbsentitySearch($keyWord){
		//得到列表
		$bbsList = Db::query("select * from t_bbs_entity bbs where bbs.name like ?",[$keyWord]);
		
		return $bbsList;
	}
	
	 /**
	 * 得到用户关注的论坛列表
	 */
	function findMemberFollowBBSList($memberId){
		$list = Db::query("select cat.name categoryName, bbs.* from t_bbs_follow follow 
        left join (t_bbs_entity bbs left join t_category cat on bbs.categoryId=cat.id ) on follow.bbsId=bbs.id 
		where follow.memberId=?",[$memberId]);
		return $list;
	}
	 
	 /**
	 * 关注
	 */
	function memberFollowbbs($memberId,$bbsId){
		//得到列表
		Db::name('t_bbs_follow')->insert(["memberId"=>$memberId,"bbsId"=>$bbsId]);
	}
	 /**
	 * 取消关注
	 */
	function memberCancelFollowBbs($memberId,$bbsId){
		Db::name("t_bbs_follow")->where("memberId",$memberId)->where("bbsId",$bbsId)->delete();
	}
	
	 /**
	 * 论坛的标签
	 */
	function bbsTags($bbsId){
		//得到列表
		$bbsTagList = Db::name('t_bbs_tag')->where("bbsId",$bbsId)->select();
		return $bbsTagList;
	}
	
	
	 /**
	 * 得到一个论坛详情
	 */
	function bbsDetail($bbsId,$memberId){
		//得到论坛
		$curbbs = Db::query("select name,poster,description,topicNum from t_bbs_entity  where id=?",[$bbsId]);
		$curbbs = !empty($curbbs)?$curbbs[0]:[];
		//得到论坛的成员数
		$countData = Db::query("select count(*) as followCount from t_bbs_follow  where bbsId=?",[$bbsId]);
		$countData = !empty($countData)?$countData[0]:[];
		$followCount = $countData["followCount"];
		
		//member是否关注了当前论坛
		$isFollow = FALSE; 
		if(!empty($memberId)){
			$countData = Db::query("select *  from t_bbs_follow  where memberId=?",[$memberId]);
			if(!empty($countData)){
				$isFollow = TRUE;
			}
		}
		
		$curbbs["followCount"] = $followCount;
		$curbbs["isFollow"] = $isFollow;
		
		return $curbbs;
	}
	
	
	 /**
	 * 得到置顶文章($memberId,$bbsId);	
	 */
	function bbsTopTopics($bbsId,$gnum,$bnum){
		$sqlSelect = "select cat.name categoryName,bbs.id bbsId,bbs.name bbsName,m.id memberId,m.realName ,m.avatar memberAvatar, topic.title topicTitle, topic.avatar topicAvatar ,
topic.likeNum,topic.clickNum,topic.replayNum,left(topic.content,30) topicContent
 from t_bbs_topic topic  
left join (t_bbs_entity bbs left join t_category cat on bbs.categoryId=cat.id) on topic.bbsId=bbs.id  
left join t_member m on topic.memberId=m.id
 ";
 
 		$sqlwhereGlobal = " where isTop=2 order by topic.orderId desc , topic.id desc limit 0,".$gnum;
 		
		$globalTopList = Db::query($sqlSelect.' '.$sqlwhereGlobal);
		
 		$sqlwhereBBS = " where isTop=1 order by topic.orderId desc , topic.id desc limit 0,".$bnum;
		
		$bbsTopList = Db::query($sqlSelect.' '.$sqlwhereBBS);
		
		$topList["globalTopList"] = $globalTopList;
		$topList["bbsTopList"] = $bbsTopList;
		
		return $topList;
	}


	 /**
	 * 首页推荐帖子接口	
	 */
	function memberTopicList($memberId,$pageNum,$limit){
		$sqlSelect = "select cat.name categoryName,bbs.id bbsId,bbs.name bbsName,m.id memberId,m.realName ,m.avatar memberAvatar, topic.title topicTitle, topic.avatar topicAvatar ,
topic.likeNum,topic.clickNum,topic.replayNum,left(topic.content,30) topicContent
 from t_bbs_topic topic  
left join (t_bbs_entity bbs left join t_category cat on bbs.categoryId=cat.id) on topic.bbsId=bbs.id  
left join t_member m on topic.memberId=m.id
 where 1=1 ";
 		$sqlwhere = " ";
 		
		if(!empty($memberId)){
			//如果存在用户的话，
			$bbsList = Db::query("select group_concat(bbsId) as inbbs from t_bbs_follow where memberId=?",[$memberId] );
			if(!empty($bbsList)){
				$inBbsStrs = $bbsList[0]["inbbs"];
				// = explode($bbsList, ",");
				if(!empty($inBbsStrs)){
					$sqlwhere = $sqlwhere." and topic.bbsId in (".$inBbsStrs.") ";	
				}
			}
		}
		
 		$sqlwhere = $sqlwhere." and  topic.isTop=true order by topic.id desc limit ".(($pageNum-1)*$limit+1)." ,  ".$limit;
 		
		
		$list = Db::query($sqlSelect.' '.$sqlwhere);
		
		return $list;
	}
	

	 /**
	 * 首页一个论坛下面的帖子列表	
	 */
	function bbsTopics($bbsId,$pageNum,$limit){
		$t = time();
		$sqlSelect = "select topic.id as topicId, cat.name categoryName,bbs.id bbsId,bbs.name bbsName,m.id memberId,m.realName ,m.avatar memberAvatar,m.pregnancyTime, 
			topic.title topicTitle, topic.avatar topicAvatar ,topic.isBest,topic.lastReplyTime,
			topic.likeNum,topic.clickNum,topic.replayNum,left(topic.content,30) topicContent,topic.createTime
			 from t_bbs_topic topic  
			left join (t_bbs_entity bbs left join t_category cat on bbs.categoryId=cat.id) on topic.bbsId=bbs.id  
			left join t_member m on topic.memberId=m.id
			 where 1=1 and topic.bbsId=?  "; //and topic.isTop=0
 		$sqlwhere = " ";
 		
 		$sqlwhere = $sqlwhere." order by topic.lastReplyTime desc limit ".(($pageNum-1)*$limit+1)." ,  ".$limit;
 		
		$list = Db::query($sqlSelect.' '.$sqlwhere,[$bbsId]);
		
		foreach($list as &$topic){
			$topicId = $topic['topicId'];
			
			$imgList = Db::query("select * from t_bbs_topic_img where topic_id=?",[$topicId]);

			$topic["imgList"] = $imgList;
		}
		
		return $list;
	}
	
	 /**
	 * 发帖
	 */
	function addTopic($topic,$imgList){
		//把imgList加入到
		$newjsons=stripslashes($imgList);
		$imglistdata = json_decode($newjsons, TRUE);
		
		//添加帖子 得到帖子ID
		$topic['createTime'] = time();
		$topicId = Db::name('t_bbs_topic')->insertGetId($topic);
		
		//保存帖子的图片
		foreach($imglistdata as $item){
			$imgpath = $item['imgpath'];
			Db::name('t_bbs_topic_img')->insertGetId(["topic_id"=>$topicId,"imgpath"=>$imgpath,"create_time"=>time()]);
		}
		
		return $topicId;
	}
	
	 /**
	 * 帖子详情
	 */
	function topicDetail($memberId,$topicId){
		$topic = Db::name('t_bbs_topic')->where("id",$topicId)->find();
		if(!empty($topic)){
			$member = Db::name('t_member')->where("id",$topic["memberId"])->find();	
		}
		if(!empty($member)){
			$member["password"]="";
		}
		$data["topic"] = $topic;
		$data["member"] = $member;
		return $data;
	}
}